package biz

import (
	"context"
	"fmt"
	pb "go-graduation/pb/router"
	"go-graduation/service/internal/pkg/output"
	"go-graduation/service/internal/service"
	"net/http"
)

func NewArticleComment() *ArticleComment {
	return &ArticleComment{}
}

type ArticleComment struct {
}

// AddComment 添加评论
func AddComment(ctx context.Context, in *pb.ArticleCommentAddRequest) pb.ArticleCommentAddResponse {
	content := in.Content
	userId := int(in.UserId)
	if userId <= 0 {
		return pb.ArticleCommentAddResponse{
			Code: output.ErrNoParamCode,
			Msg:  output.ErrNoParam.Error(),
		}
	}

	err := service.NewArticleComment().AddArticleCommentToMQ(userId, content)
	if err != nil {
		return pb.ArticleCommentAddResponse{
			Code: output.ErrNoRecordCode,
			Msg:  output.ErrNoRecord.Error(),
		}
	}
	return pb.ArticleCommentAddResponse{
		Code: http.StatusOK,
		Msg:  "success",
	}
}

// GetArticleCommentByPage 获取全部文章
func GetArticleCommentByPage(ctx context.Context, in *pb.ArticleCommentListRequest) pb.ArticleCommentListResponse {
	var res pb.ArticleCommentListResponse
	var page int = 1
	if in.Page > 0 {
		page = int(in.Page)
	}
	list, err := service.NewArticleComment().GetArticleCommentByPage(page)
	if err != nil {
		//output.NewOutput().Failure(ctx, output.ErrNoRecordCode, output.ErrNoRecord.Error())
		return res
	}

	fmt.Printf("list = %#v", list)

	return pb.ArticleCommentListResponse{
		Data: &pb.ArticleCommentListResponse_Data{
			List: list,
		},
		Code: http.StatusOK,
		Msg:  "success",
	}
}
